package com.sun.memo.web.controller;

import com.sun.memo.entity.BackLog;
import com.sun.memo.entity.User;
import com.sun.memo.service.IBackLogService;
import com.sun.memo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wys

 **/
@Controller
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IBackLogService backLogService;

    @RequestMapping("/login")
    public String Login(String name, String password, HttpSession session) {
        //用户名密码都对
        //这里拿到对应用户的logs，放到session里
        User user = userService.findByUsername(name, password);
        Integer id = user.getId();
        List<BackLog> backLogs = backLogService.findLogsByDay("today",id);
        session.setAttribute("todayBacklogs",backLogs);
        session.setAttribute("user", user);
        return "index";
    }

    @ResponseBody
    @RequestMapping("/register")
    public String Register(String name, String password) {
        return userService.saveUser(new User(name, password));
    }
}
